﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.Example.MVC.Filter;
using TjZheng.Example.MVC.Models;
using TjZheng.Mvc;
using TjZheng.Framework;

namespace TjZheng.Example.MVC.Controllers
{
    //[RainFilter("控制器")]
    //[EncryptionFilter(true)]
    public class InterfaceController : TjZhengController
    {
        [EncryptionFilter(true)]
        public ActionResult Encrypt()
        {
            return Json(new ZooEntity { bar = "bar", baz = "baz", zoo = "zoo" }, JsonRequestBehavior.AllowGet);
            //return Jsonp(new ZooEntity { bar = "bar", baz = "baz", zoo = "zoo" }, JsonRequestBehavior.AllowGet);
            //return Content("124");
        }

        public ActionResult Decrypt()
        {
            int SignTime = TimeHelper.ConvertToSecondStamp(DateTime.Now);
            int TimeTout = 30;
            //约定的密钥
            string DecryptKey = "";
            //签名
            string Signature = (SignTime.ToString() + TimeTout.ToString() + DecryptKey).EncodeMD5();

            InvokeResult objInvokeResult = HttpHelper.CreateHttpGet<InvokeResult>("http://tjzheng.example.com/Interface/Encrypt?SignTime=" + SignTime + "&TimeTout=" + TimeTout + "&Signature=" + Signature);
            if (objInvokeResult.ResultCode == "Success")
            {
                objInvokeResult.Data = DecryptHandle(objInvokeResult.Data.ToString()).JsonDeserialize();
            }
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        //[RainFilter("动作1")]
        //[RainFilter("动作2")]
        //public void Test()
        //{
        //    Response.Write("InterfaceController.Test");
        //}

        /// <summary>
        /// 自定义加密算法
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected string DecryptHandle(string data)
        {
            string DecryptKey = "";
            if (DecryptKey.IsNull())
            {
                return data.DecodeBase64();
            }
            return data.DecodeBase64().Replace(DecryptKey, "");
        }
    }
}
